﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using corpus_platform.Data;
using Prism.Events;

namespace corpus_platform.ViewModels
{
    public class OntologyViewModel : BindableBase, INodeStructure
    {
        public OntologyViewModel(ListedOntology ontology)
        {
            Name = ontology.Name;
            Information = ontology.Information;
            Root = ontology.Root;
            parent = Root;
            Root.add(this);
        }

        private INodeContainer parent;
        public RootViewModel Root { get; }
        public StructureType Type => StructureType.Ontology;
        public string Name { get; }
        public string Information { get; }

        private bool allowDrag = true;
        public bool AllowDrag
        {
            get { return allowDrag; }
            set { SetProperty(ref allowDrag, value); }
        }

        public void ChangeParentTo(INodeContainer container)
        {
            if (container.add(this) == Result.Success)
            {
                parent.remove(this);
                parent = container;
            }
        }

        private DelegateCommand commandRemove;
        public DelegateCommand CommandRemove =>
            commandRemove ?? (commandRemove = new DelegateCommand(ExecuteCommandRemove, CanExecuteCommandRemove));

        void ExecuteCommandRemove()
        {
            Root.remove(this);
        }

        bool CanExecuteCommandRemove()
        {
            if (parent is RootViewModel)
            {
                return true;
            }
            else
            {
                INodeFrameElement frameElement = (INodeFrameElement)parent;
                if (frameElement.Frame.IsInstance)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        public object GetValue(MetaInformation metaInformation)
        {
            return Name;
        }
    }
}
